//
//  listHelper.m
//  JZMCFM
//
//  Created by lanou3g on 15/10/31.
//  Copyright (c) 2015年 陈卓. All rights reserved.
//

#import "listHelper.h"
#import "focusImagesModel.h"
#import "programAndhostModel.h"
#import "FirstKResults.h"

static listHelper *shared;

@interface listHelper ()

@property(nonatomic,strong) NSMutableArray *mutableArray;
@property(nonatomic,strong) NSMutableArray *programMutArray;
@property(nonatomic,strong) NSMutableArray *hostMutArray;
@property(nonatomic,strong) NSMutableArray *firstKResultsMutArray;
@end

@implementation listHelper

+(instancetype)sharedInstance{
    
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        
        if (shared == nil) {
            shared = [[listHelper alloc]init];
        }
    });
    return shared;
}

#pragma mark 懒加载

-(NSMutableArray*)mutableArray{
    
    if (_mutableArray == nil) {
        _mutableArray = [NSMutableArray array];
    }
    return _mutableArray;
}

-(NSMutableArray*)programMutArray{
    
    if (_programMutArray == nil) {
        _programMutArray = [NSMutableArray array];
    }
    return _programMutArray;
}

-(NSMutableArray*)hostMutArray{
    
    if (_hostMutArray == nil) {
        _hostMutArray = [NSMutableArray array];
    }
      return _hostMutArray;
}

-(NSMutableArray*)firstKResultsMutArray{
    
    if (_firstKResultsMutArray == nil) {
        _firstKResultsMutArray = [NSMutableArray array];
    }
    return _firstKResultsMutArray;
}

-(NSArray*)focusImageArray{
    
    return [self.mutableArray copy];
}

-(NSArray*)programArray{
    
    return [self.programMutArray copy];
}

-(NSArray*)hostArray{
    
    return [self.hostMutArray copy];
}

-(NSArray*)firstKResultsArray{
    return [self.firstKResultsMutArray copy];
}
-(void)downloadDataFromNetWork:(void(^)())results{
    
    dispatch_async(dispatch_get_global_queue(0, 0), ^{
    //菊花开始
        [SVProgressHUD showWithStatus:@"拼命加载中"maskType:SVProgressHUDMaskTypeClear];
        NSString *strig = listhost;
        NSURL *url = [NSURL URLWithString:strig];
        NSURLRequest *request = [NSURLRequest requestWithURL:url];
        
        [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
            
       //解析数据
            NSDictionary *result = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];

         //接受focusImage的数据
            NSDictionary *focusImages = result[@"focusImages"];

            //定义数组接受list的数据
            NSArray *listArray = focusImages[@"list"];

            //循环,添加
            for (NSDictionary *dict in listArray) {
                focusImagesModel *model = [[focusImagesModel alloc]init];
                
                [model setValuesForKeysWithDictionary:dict];
                [self.mutableArray addObject:model];
            }


            //解析节目榜单数据
            NSArray *datasArray = result[@"datas"];
            //接受第一个字典的值
            NSDictionary *datas1Dict = datasArray[0];
            NSArray *list1array = datas1Dict[@"list"];
            for (NSDictionary *dict  in list1array) {
                programAndhostModel *model = [[programAndhostModel alloc]init];
                [model setValuesForKeysWithDictionary:dict];
                [self.programMutArray addObject:model];
                
                //接受firstKResults数组里面的数据
                NSArray *firstArray = dict[@"firstKResults"];
                for (NSDictionary *dict in firstArray) {
                    FirstKResults *model = [[FirstKResults alloc]init];
                    [model setValuesForKeysWithDictionary:dict];
                    [self.firstKResultsMutArray addObject:model];
                }
                
            }
            
            //解析主播榜单数据
            //接受第二个字典的值
            NSDictionary *datas2Dict = datasArray[1];
            NSArray *list2array = datas2Dict[@"list"];
            for (NSDictionary *dict  in list2array) {
                programAndhostModel *model = [[programAndhostModel alloc]init];
                [model setValuesForKeysWithDictionary:dict];
                [self.hostMutArray addObject:model];
            }
        dispatch_async(dispatch_get_main_queue(), ^{
            results();
            
        });
        //菊花结束
            [SVProgressHUD showSuccessWithStatus:@"加载完成"];
            
      }];
            
    });
    
}










@end
